#ifdef HOME_EASY_SEND
void UserPlugin_HE_Command(int Par1, int Par2)
{
  if ((Par1 >= 128) && (Par1 <= 144))
  {
    //HomeEasy_2_RawSignal(command2event(Settings.Unit, CMD_KAKU_NEW,Par1,Par2));
#if NODO_CLASSIC
    HomeEasy_2_RawSignal(command2event(CMD_KAKU_NEW,Par1,Par2));
#else
    HomeEasy_2_RawSignal(command2event(0, CMD_KAKU_NEW,Par1,Par2));
#endif
    RawSendRF();
  }
}
#endif

#ifdef HOME_EASY_RECV
/*********************************************************************************************\ 
 * Called directly after RF message received
 \*********************************************************************************************/
unsigned long RawSignal_2_UserPlugin_HE(int Port)
{

  unsigned long code = 0;

  if ((code == 0) && RawSignal.Number == 116) code = HomeEasy__EU_Decode();

  // return 0    = Nodo continues to analyze rawsignal
  // return 1    = Nodo stops processing for this event
  // return <> 0 = Node continues with returncode as Event
  return code; 
}

/*********************************************************************************************\
 * Analyses Home Easy Messages and convert these into NewKaku compatible eventcode
 * Only new EU devices with automatic code system are supported
 * Only  On / Off status is decoded, no DIM values
 * Anly tested with Home Easy HE300WEU transmitter, doorsensor and PIR sensor
 * Turns an Home Easy signal into unique newkaku code, so Home Easy becomes more or less compatible with KAKU
 * Home Easy message structure, by analyzing bitpatterns so far ...
 * AAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBCCCCDDEEFFFFFFG
 *       A = startbits/preamble, B = address, C = ?, D = Command, E = ?, F = Channel, G = Stopbit
 \*********************************************************************************************/
unsigned long HomeEasy__EU_Decode(void)
{
  unsigned long address = 0;
  unsigned long bitstream = 0;
  int counter = 0;
  byte rfbit =0;
  byte state = 0;
  unsigned long channel = 0;

  // valid messages are 116 pulses          
  if (RawSignal.Number != 116) return 0L;

  for(byte x=1;x<=RawSignal.Number;x=x+2)
  {
    if ((RawSignal.Pulses[x]<500) & (RawSignal.Pulses[x+1]>500)) 
      rfbit = 1;
    else
      rfbit = 0;

    if ((x>=23) && (x<=86)) address = (address << 1) | rfbit;
    if ((x>=87) && (x<=114)) bitstream = (bitstream << 1) | rfbit;

  }
  state = (bitstream >> 8) & 0x3;
  channel = (bitstream) & 0x3f;

  // Add channel info to base address, first shift channel info 6 positions, so it can't interfere with bit 5
  channel = channel << 6;
  address = address + channel;

  // Set bit 5 based on command information in the Home Easy protocol
  if (state == 1) address = address & 0xFFFFFEF;
  else address = address | 0x00000010;

  return (address&0x0fffffff) | (unsigned long)(SIGNAL_TYPE_NEWKAKU) << 28;
}
#endif

/*********************************************************************************************\
 * Home_Easy versie:Deze routine berekent de RAW pulsen uit een CMD_NEWKAKU plaatst deze in de buffer RawSignal
 \*********************************************************************************************/
#ifdef HOME_EASY_SEND

#define HomeEasy_LongLow    0x490    // us
#define HomeEasy_ShortHigh  200      // us
#define HomeEasy_ShortLow   150      // us

void HomeEasy_2_RawSignal(unsigned long CodeNodo)
{
  unsigned long bitstream=0L;
  byte address = 0;
  byte channel = 0;
  byte channelcode = 0;
  byte command = 0;
  byte i=1; // bitcounter in stream
  byte y; // size of partial bitstreams

  address = (CodeNodo >> 12) & 0x7;   // 3 bits address (higher bits from newkaky address, bit 7 not used
  channel = (CodeNodo >> 8) & 0xF;    // 4 bits channel (lower bits from newkaku address
  command = (CodeNodo) & 0xF;    // 12 = on, 0 = off

  if (channel == 0) channelcode = 0x8E;
  if (channel == 1) channelcode = 0x96;
  if (channel == 2) channelcode = 0x9A;
  if (channel == 3) channelcode = 0x9C;
  if (channel == 4) channelcode = 0xA6;
  if (channel == 5) channelcode = 0xAA;
  if (channel == 6) channelcode = 0xAC;
  if (channel == 7) channelcode = 0xB2;
  if (channel == 8) channelcode = 0xB4;
  if (channel == 9) channelcode = 0xB8;
  if (channel == 10) channelcode = 0xC6;
  if (channel == 11) channelcode = 0xCA;
  if (channel == 12) channelcode = 0xCC;
  if (channel == 13) channelcode = 0xD2;
  if (channel == 14) channelcode = 0xD4;
  if (channel == 15) channelcode = 0xD8;

  y=11; // bit uit de bitstream, startbits
  bitstream = 0x63C;
  for (i=1;i<=22;i=i+2)
  {
    RawSignal.Pulses[i] = HomeEasy_ShortHigh;
    if((bitstream>>(y-1))&1)          // bit 1
        RawSignal.Pulses[i+1] = HomeEasy_LongLow;
    else                              // bit 0
    RawSignal.Pulses[i+1] = HomeEasy_ShortLow;
    y--;
  }

  y=32; // bit uit de bitstream, address
  bitstream = 0xDAB8F56C + address;

  for (i=23;i<=86;i=i+2)
  {
    RawSignal.Pulses[i] = HomeEasy_ShortHigh;
    if((bitstream>>(y-1))&1)          // bit 1
        RawSignal.Pulses[i+1] = HomeEasy_LongLow;
    else                              // bit 0
    RawSignal.Pulses[i+1] = HomeEasy_ShortLow;
    y--;
  }

  y=15; // bit uit de bitstream, other stuff

  bitstream = 0x5C00;  // bit 10 on, bit 11 off indien OFF
  if (command == 0) bitstream = 0x5A00;

  bitstream = bitstream + channelcode;

  for (i=87;i<=116;i=i+2)
  {
    RawSignal.Pulses[i] = HomeEasy_ShortHigh;
    if((bitstream>>(y-1))&1)          // bit 1
        RawSignal.Pulses[i+1] = HomeEasy_LongLow;
    else                              // bit 0
    RawSignal.Pulses[i+1] = HomeEasy_ShortLow;
    y--;
  }

  RawSignal.Pulses[116]=0;
  RawSignal.Number=116; // aantal bits*2 die zich in het opgebouwde RawSignal bevinden
}

#endif

